package com.shell.scheduler;

import com.shell.scheduler.job.po.SchdJob;
import com.shell.scheduler.job.service.ScheJobService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.springframework.beans.factory.annotation.Autowired;

public class JobStateListener implements JobListener {
	private Log logger = LogFactory.getLog(getClass());
	@Autowired
	private ScheJobService scheJobService;

	public String getName() {
		return "jobStateListener";
	}

	public void jobToBeExecuted(JobExecutionContext context) {
		this.logger.debug("JobStateListener.jobToBeExecuted");
		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		SchdJob job = (SchdJob) jobDataMap.get("SCHE_JOB");
		job.setJobState("R");
		this.scheJobService.updateAll(job);
	}

	public void jobExecutionVetoed(JobExecutionContext context) {
		this.logger.debug("JobStateListener.jobExecutionVetoed");
	}

	public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
		this.logger.debug("JobStateListener.jobWasExecuted");
		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		SchdJob job = (SchdJob) jobDataMap.get("SCHE_JOB");
		job.setJobState("E");
		this.scheJobService.updateAll(job);
	}
}
